/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2011-4-19
 */
package net;

import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import code.extract.FileDealer;

public class GooglePageRobustParser {
	public static void main(String args[]){
		
		try {
			String pageContent = FileDealer.getFileContent("C:/Users/ftc/Desktop/search_google_hit.htm");
			ArrayList<String> testList = getResultLinkList(pageContent);
			for (int i=0; i<testList.size(); ++i){
				System.out.println(testList.get(i));
			}
			System.out.println(testList.size());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static ArrayList<String> getResultLinkList(String pageContent){
		ArrayList<String> linkList = new ArrayList<String>();
		String str_pattern = "<h3 [^>]*?class=\"{0,1}r\"{0,1}[^>]*?>.*?</h3>";
		Pattern pattern = Pattern.compile(str_pattern, Pattern.DOTALL|Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(pageContent);
		while (matcher.find()){
			String thisPartition = new String(pageContent.substring(matcher.start(), matcher.end()));
			int href_start = thisPartition.indexOf("href=\"");
			if (href_start < 0){
				continue;
			}
			int href_end = thisPartition.indexOf("\"", href_start + 6);
			if (href_end < 0){
				continue;
			}
			String thisLink = new String(thisPartition.substring(href_start + 6, href_end));
			linkList.add(thisLink);
		}
		return linkList;
	}
}
